package com.woniuxy.ticketmovie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.ticketmovie.entity.Ticket_movie;
import com.woniuxy.ticketmovie.entity.Ticket_movie_type;
import com.woniuxy.ticketmovie.exception.TypeNameExsitException;
import com.woniuxy.ticketmovie.mapper.Ticket_movieMapper;
import com.woniuxy.ticketmovie.mapper.Ticket_movie_typeMapper;
import com.woniuxy.ticketmovie.service.Ticket_movieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author movie
 * @since 2021-02-21
 */
@Service
public class Ticket_movieServiceImpl extends ServiceImpl<Ticket_movieMapper, Ticket_movie> implements Ticket_movieService {

    @Autowired
    private Ticket_movieMapper movieMapper;

    @Autowired
    private Ticket_movie_typeMapper ticketMovieTypeMapper;

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public void add(Ticket_movie movie,int[] TypeIds) {

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("name",movie.getName());


        if (movieMapper.selectCount(queryWrapper)==0){
            movieMapper.insert(movie);
        }else {
            throw new TypeNameExsitException("");
        }
        for (int typeId : TypeIds) {

            Ticket_movie_type movie_type=new Ticket_movie_type();
            movie_type.setTypeid(typeId);
            movie_type.setMovieid(movie.getId());
            ticketMovieTypeMapper.insert(movie_type);
        }


    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED)
    public void delete(Integer id) {

        movieMapper.deleteById(id);
        ticketMovieTypeMapper.deleteById(id);

    }
}
